/*
 * YuebaoDownloadTask.cpp
 *
 *  Created on: 2011-3-11
 *      Author: 
 */

#include "YuebaoDownloadTask.h"
#include "fWml.h"
#include "ProjectsDefines.h"
#include "XmlNodeCreater.h"
#include "BetterUserHBufC.h"

YuebaoDownloadTask* YuebaoDownloadTask::NewLC( )
{
    YuebaoDownloadTask* self = new ( ELeave ) YuebaoDownloadTask( );
    CleanupStack::PushL( self );
    self->ConstructL( );
    return self;
}

YuebaoDownloadTask* YuebaoDownloadTask::NewL( )
{
    YuebaoDownloadTask* self = YuebaoDownloadTask::NewLC( );
    CleanupStack::Pop( ); // self;
    return self;
}

YuebaoDownloadTask::YuebaoDownloadTask( )
{

}

void YuebaoDownloadTask::ConstructL( )
{

}

YuebaoDownloadTask::~YuebaoDownloadTask( )
{
    SAFE_DELETE( _name );
    SAFE_DELETE( _url );
    SAFE_DELETE( _datafile );
}
_LIT( kTaskIDNode, "id");
_LIT( kTaskNameNode, "name");
_LIT( kTaskUrlNode, "url");
_LIT( kTaskDataFileNode, "data_file");
_LIT( kTaskTotalNode, "total");
_LIT( kTaskDownloadedNode, "downloaded");
_LIT( kTaskStatusNode, "status");
_LIT( kTaskTimesNode, "retry_times");

YuebaoDownloadTask* YuebaoDownloadTaskParser::Parse( CElement* elementGoods )
{
    if( elementGoods == 0 || elementGoods->ElementCount( ) == 0 )
    {
        return NULL;
    }
    YuebaoDownloadTask* task = YuebaoDownloadTask::NewLC( );

    task->_id = BetterUtility::GetElementTextAsInt( elementGoods->GetElement( kTaskIDNode ) );
    task->_name = BetterUtility::GetElementText( elementGoods->GetElement( kTaskNameNode ) );
    task->_url = BetterUtility::GetElementText( elementGoods->GetElement( kTaskUrlNode ) );
    task->_datafile = BetterUtility::GetElementText( elementGoods->GetElement( kTaskDataFileNode ) );
    task->_status = ( YuebaoDownloadTask::TDownloadStatus )BetterUtility::GetElementTextAsInt( elementGoods->GetElement( kTaskStatusNode ) );
    task->_total = BetterUtility::GetElementTextAsInt( elementGoods->GetElement( kTaskTotalNode ) );
    task->_downloaded = BetterUtility::GetElementTextAsInt( elementGoods->GetElement( kTaskDownloadedNode ) );
    task->_retry_times = BetterUtility::GetElementTextAsInt( elementGoods->GetElement( kTaskTimesNode ) );

    CleanupStack::Pop( ); // goods;
    return task;
}
_LIT( kTaskNodeStart,"<task>" );
_LIT( kTaskNodeEnd ,"</task>");

void YuebaoDownloadTaskParser::BuildXmlNode( YuebaoDownloadTask* task, TDes& buffer )
{
    //buffer.SetLength( 0 );
    if( !task )
    {
        return;
    }

    buffer.Append( kTaskNodeStart );
    //DebugLog("");
    XmlNodeCreater::BuildIntNode( kTaskIDNode, task->_id, buffer );
    //DebugLog("");
    XmlNodeCreater::BuildTextNode( kTaskNameNode, task->_name, buffer );
    //DebugLog("");
    XmlNodeCreater::BuildTextNode( kTaskUrlNode, task->_url, buffer );
    //DebugLog("");
    XmlNodeCreater::BuildTextNode( kTaskDataFileNode, task->_datafile, buffer );
    //DebugLog("");
    XmlNodeCreater::BuildIntNode( kTaskStatusNode, task->_status, buffer );
    //DebugLog("");
    XmlNodeCreater::BuildIntNode( kTaskTotalNode, task->_total, buffer );
    //DebugLog("");
    XmlNodeCreater::BuildIntNode( kTaskDownloadedNode, task->_downloaded, buffer );
    //DebugLog("");
    XmlNodeCreater::BuildIntNode( kTaskTimesNode, task->_retry_times, buffer );
    //DebugLog("");

    buffer.Append( kTaskNodeEnd );
}
